package org.example.mall.admin.configuration.auth.code;

import org.example.mall.admin.constant.auth.AuthTypeConst;
import org.example.mall.admin.configuration.exception.BaseException;
import org.example.mall.admin.util.R;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author hzq
 * @date 2021/12/23 14:28
 */
@Component
public class PhoneSendCodeHandler extends AbstractSendCodeHandler {

    private static final List<String> SUPPORT_SCENES = new ArrayList<>(Arrays.asList(ScenesConst.LOGIN, ScenesConst.BIND));

    @Override
    protected R<Object> handler(String scenes, String principal, String handlerType) {

        // 保存验证码到redis
        Code code = saveCodeToRedis(scenes, principal, handlerType);

        // todo 使用第三短信sdk发送短信验证码信息


        return R.ofSuccess("发送手机登录验证码成功", code);
    }

    @Override
    public Object checkScenes(String scenes, String principal, String handlerType) {

        // 用户手机号码登录时发送手机验证码 判断是否有该用户信息
        if (SUPPORT_SCENES.get(0).equals(scenes)) {
            if (getAuth(principal, handlerType) == null) {
                throw new BaseException("发送验证码失败，用户信息有误");
            }
        }

        return null;
    }

    @Override
    protected boolean supportType(String handlerType) {
        return AuthTypeConst.PHONE.equals(handlerType);
    }

    @Override
    protected boolean supportScenes(String scenes) {
        return SUPPORT_SCENES.contains(scenes);
    }
}
